package com.googlecode.agileplugins.cruisewatcher;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CruiseControlFeedParser implements FeedParser {
    private static final String FAILED = "FAILED!";

    public LatestBuildState parseLatestBuildResult(CruiseBuild cruiseBuild, String rssfeed) {
        Pattern p = Pattern.compile("<item>\\s+<title>(.*?), (" + FAILED + "|passed)</title>");
        Matcher matcher = p.matcher(rssfeed);
        if (!matcher.find()) {
            throw new RuntimeException("Could not parse RSS file.");
        }
        String date = matcher.group(1);
        String state = matcher.group(2);
        return new LatestBuildState(date, state.equals(FAILED));
    }
}
